//
//  SettingViewController.m
//  Marketing
//
//  Created by HanenDev on 16/2/25.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "SettingViewController.h"
#import "FirstViewController.h"
#import "SetCell.h"
#import "ResetPassViewController.h"
#define STARTX [UIView getWidth:10]
#define LINEW KSCreenW - 2*STARTX
#define ROWH 50.0f

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    NSMutableArray *_titleArray;
    
    UIView         *aboutUsView;
    UIImageView    * bgImage;
    UIButton       * QRbutton;
}
@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = graySectionColor;
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel: @"设置"];
    
    _titleArray = [NSMutableArray arrayWithArray:@[@[@"账号与安全"],@[@"意见反馈",@"关于我们"]]];
    [self addBottomBtn];
    [self createUI];
    [self createQRview];
}
- (void)createUI{
    
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH -64 -70) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.scrollEnabled = NO;
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.rowHeight = ROWH;
    tableView.backgroundColor = graySectionColor;
    [self.view addSubview:tableView];
}
- (void)addBottomBtn{
    
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, KSCreenH - 70 -64, KSCreenW, 70)];
    footerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:footerView];
    
    UIButton *outLoginBtn = [[UIButton alloc]initWithFrame:CGRectMake(13, 8, footerView.width - 20, footerView.height - 10)];
    [outLoginBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [outLoginBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_normal.png"] forState:UIControlStateNormal];
    [outLoginBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_press.png"] forState:UIControlStateHighlighted];
    [outLoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [outLoginBtn addTarget:self action:@selector(clickToOutLogin:) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:outLoginBtn];
    
    UIView *line3 = [ViewTool getLineViewWith:CGRectMake(0, 0, KSCreenW, 1) withBackgroudColor:grayLineColor];
    [footerView addSubview:line3];
}
- (void)createQRview{
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    
    aboutUsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH)];
    aboutUsView.backgroundColor = [UIColor blackColor];
    aboutUsView.alpha = 0.5;
    aboutUsView.hidden = YES;
    [window addSubview:aboutUsView];
    
    bgImage = [[UIImageView alloc]initWithFrame:CGRectMake([UIView getWidth:25], [UIView getHeight:70], KSCreenW - 2*[UIView getWidth:25], KSCreenH - 2.5*[UIView getHeight:70])];
    bgImage.image = [UIImage imageNamed:@"HY_popup_about_us"];
    bgImage.backgroundColor = [UIColor whiteColor];
    bgImage.hidden = YES;
    bgImage.layer.cornerRadius = 5;
    bgImage.layer.masksToBounds = YES;
    [window addSubview:bgImage];
    
    QRbutton = [[UIButton alloc]initWithFrame:CGRectMake((KSCreenW -40) / 2, bgImage.maxY + [UIView getHeight:20], 40, 40)];
    [QRbutton setImage:[UIImage imageNamed:@"HY_btn_close"] forState:UIControlStateNormal];
    [QRbutton addTarget:self action:@selector(hiddenQRView) forControlEvents:UIControlEventTouchUpInside];
    QRbutton.hidden = YES;
    [window addSubview:QRbutton];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toHiddenQRView:)];
    [aboutUsView addGestureRecognizer:tapGesture];
    
}
- (void)hiddenQRView{
    [UIView animateWithDuration:0.25 animations:^{
        aboutUsView.hidden = YES;
        bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}
- (void)toHiddenQRView:(UITapGestureRecognizer *)tap{
    [UIView animateWithDuration:0.25 animations:^{
        aboutUsView.hidden = YES;
        bgImage.hidden = YES;
        QRbutton.hidden = YES;
    }];
}

#pragma mark ---- tableview的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return 2;
    }
    return 0;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    SetCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell=[[SetCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.titleLabel.text = _titleArray[indexPath.section][indexPath.row];
        cell.lineView.frame = CGRectMake(0, ROWH - 1, KSCreenW, 1);
    }else if (indexPath.section == 1){
        cell.subLabel.text = @"";
        cell.titleLabel.text = _titleArray[indexPath.section][indexPath.row];
        
        if (indexPath.row == 1) {
            cell.lineView.frame = CGRectMake(0, ROWH - 1, KSCreenW, 1);
        }else{
            cell.lineView.frame = CGRectMake([UIView getWidth:10], ROWH - 1, KSCreenW - [UIView getWidth:10], 1);
        }
    }
    
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *views= [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, 20)];
    views.backgroundColor = graySectionColor;
    
    UIView *line = [ViewTool getLineViewWith:CGRectMake(0, 20 - 1, KSCreenW, 1) withBackgroudColor:grayLineColor];
    [views addSubview:line];
    
    return views;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section ==0) {
//        //账号与安全
//        ResetPassViewController *vc = [[ResetPassViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            //意见反馈
            [self alertWithString:@"感谢您提出的宝贵意见"];
        }else if (indexPath.row == 1){
            //关于我们
            [UIView animateWithDuration:0.25 animations:^{
                aboutUsView.hidden = NO;
                bgImage.hidden = NO;
                QRbutton.hidden = NO;
            }];
        }
    }
    
}
- (void)alertWithString:(NSString *)str{
    
    ZLAlertView *alert = [[ZLAlertView alloc] initWithTitle:@"" message:str];
    [alert addBtnTitle:@"OK" action:^{
        
    }];
    [alert showAlertWithSender:self];
    
}
- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}
//退出登录
- (void)clickToOutLogin:(UIButton *)btn{
    
    
    ZLAlertView *alert = [[ZLAlertView alloc] initWithTitle:@"提示" message:@"是否退出登录"];
    [alert addBtnTitle:@"取消" action:^{
        
    }];
    [alert addBtnTitle:@"确定" action:^{
        //删除本地的token和uid;
        NSDictionary * dic =@{@"token":TOKEN,@"uid":@(UID)};
        [DataTool postWithUrl:LOGOUT_URL parameters:dic success:^(id data) {
            NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSLog(@"%@",jsonDic);
            
            int code = [[jsonDic objectForKey:@"code"] intValue];
            if (code == 100) {
                
                NSLog(@"退出成功");
            }else{
                NSLog(@"退出失败");
                //                    [self.view makeToast:@"网络连接失败"];
            }
        } fail:^(NSError *error) {
            NSLog(@"%@+++++++++",error);
        }];
        
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"token"];
        [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"uid"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        NSLog(@"%d======%@",UID,TOKEN);
        
        [self.navigationController pushViewController:[[FirstViewController alloc] init] animated:YES];
    }];
    
    [alert showAlertWithSender:self];
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
